<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Contracts\Validation\Validator;


class UpdateProjectRequest extends FormRequest
{
    // 1、创建表单出现的错误，都会出现在自定义的create错误包里面
    // protected $errorBag = 'update';
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            // projects表的name字段，必填|唯一
            'name' => [
                'required',
                Rule::unique('projects')->ignore($this->route('project'))->where(function ($query){
                    return $query->where('user_id', request()->user()->id);
                })
            ],
            // projects表的thumbnail字段，图片|尺寸260×100
            'thumbnail' => 'image|dimensions:min_width=260,min_height=100,thumbnail'
        ];
    }

    // 因为错误信息是英文，我们当然要改成中文了，自定义下面的方法就可以啦
    public function messages()
    {
        return [
            'name.required'=>'项目名称是必填的～',
            'name.unique'=>'项目名称必须是唯一的～'
        ];
    }

    protected function failedValidation(Validator $validator)
    {
        $this->errorBag = 'update-'. $this->route('project');
        parent::failedValidation($validator);
    }
}
